package rexsee.core.browser.clazz;

import rexsee.core.browser.Browser;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;

public class TouchListener implements OnTouchListener {

	private final Browser mBrowser;
	private final GestureDetector mGestureDetector;

	public TouchListener(Browser browser, GestureDetector gestureDetector) {
		mBrowser = browser;
		mGestureDetector = gestureDetector;
	}

	@Override
	public boolean onTouch(View v, MotionEvent event) {
		boolean listenToTouchMove = mBrowser.styleSheet.event_touchmove.equalsIgnoreCase("true");
		int action = event.getAction();
		if (action == MotionEvent.ACTION_DOWN) {
			mBrowser.touchEventInfo.basicAssignment(TouchEventInfo.EVENT_DOWN, event);
			mBrowser.eventList.run(Browser.EVENT_ONTOUCHDOWN);
		} else if (action == MotionEvent.ACTION_UP) {
			mBrowser.touchEventInfo.basicAssignment(TouchEventInfo.EVENT_UP, event);
			mBrowser.eventList.run(Browser.EVENT_ONTOUCHUP);
		} else if (action == MotionEvent.ACTION_MOVE && listenToTouchMove) {
			mBrowser.touchEventInfo.basicAssignment(TouchEventInfo.EVENT_MOVE, event);
			mBrowser.eventList.run(Browser.EVENT_ONTOUCHMOVE);
			return true;
		} else if (action == MotionEvent.ACTION_CANCEL && listenToTouchMove) {
			mBrowser.touchEventInfo.basicAssignment(TouchEventInfo.EVENT_CANCEL, event);
			mBrowser.eventList.run(Browser.EVENT_ONTOUCHCANCEL);
			return true;
		}
		return (mGestureDetector != null) ? mGestureDetector.onTouchEvent(event) : false;
	}

}
